package disease

import (
	"fmt"
	"gitcode.com/G-YT/ANN/server/global"
	adminReq "gitcode.com/G-YT/ANN/server/model/admin/request"
	"gitcode.com/G-YT/ANN/server/model/body"
	"gitcode.com/G-YT/ANN/server/model/catering"
	"gitcode.com/G-YT/ANN/server/model/common/response"
	"gitcode.com/G-YT/ANN/server/model/disease"
	diseaseReq "gitcode.com/G-YT/ANN/server/model/disease/request"
	diseaseRes "gitcode.com/G-YT/ANN/server/model/disease/response"
	"gitcode.com/G-YT/ANN/server/service/user"
	"gitcode.com/G-YT/ANN/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"strconv"
	"strings"
)

type DiseaseApi struct {
}

var UserService = new(user.UserService)

// GetDiseaseTypeList 獲取病症类型列表
// @Tags Disease
// @Summary 獲取病症类型列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /api/Food/getBzlxList [get]
func (diseaseApi *DiseaseApi) GetDiseaseTypeList(c *gin.Context) {
	if list, err := DiseaseService.GetDiseaseTypeList(); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.Result(200, list, "獲取成功", c)
	}
}

// GetDiseaseList 獲取病症列表
// @Tags GetDiseaseList
// @Summary 獲取病症列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /Food/getBzList [get]
func (diseaseApi *DiseaseApi) GetDiseaseList(c *gin.Context) {
	var db diseaseReq.BzsRes
	err := c.ShouldBindJSON(&db)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	ids := strings.Split(db.Ids, ",")
	if list, err := DiseaseService.GetDiseaseList(ids); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.Result(200, list, "獲取成功", c)
	}
}

// GetDiseaseRank 獲取病症排行【超管】
// @Tags GetDiseaseRank
// @Summary 獲取病症排行【超管】
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /disease/getDiseaseRank [get]
func (diseaseApi *DiseaseApi) GetDiseaseRank(c *gin.Context) {
	var pageInfo diseaseReq.DiseaseRank
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := DiseaseService.GetDiseaseRank(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:  list,
			Total: total,
		}, "獲取成功", c)
	}
}

// GetScDiseaseRank 獲取病症排行【服务中心】
// @Tags GetDiseaseRank
// @Summary 獲取病症排行【服务中心】
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /disease/getScDiseaseRank [get]
func (diseaseApi *DiseaseApi) GetScDiseaseRank(c *gin.Context) {
	var pageInfo diseaseReq.DiseaseRank
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	pageInfo.ServiceCenterID = utils.GetUserID(c)
	if list, total, err := DiseaseService.GetScDiseaseRank(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:  list,
			Total: total,
		}, "獲取成功", c)
	}
}

// ExportDiseaseRankExcel 导出病症排行Excel【超管】
// @Tags ExportDiseaseRankExcel
// @Summary 导出病症排行Excel【超管】
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /disease/exportDiseaseRankExcel [get]
func (diseaseApi *DiseaseApi) ExportDiseaseRankExcel(c *gin.Context) {
	var pageInfo diseaseReq.DiseaseRank
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if file, name, err := DiseaseService.ExportDiseaseRankExcel(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", name+utils.RandomString(6)+".xlsx")) // 对下载的文件重命名
		c.Header("success", "true")
		c.Data(http.StatusOK, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file.Bytes())
	}
}

// ExportScDiseaseRankExcel 导出服务中心病症排行Excel【服务中心】
// @Tags ExportScDiseaseRankExcel
// @Summary 导出服务中心病症排行Excel【服务中心】
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /disease/exportScDiseaseRankExcel [get]
func (diseaseApi *DiseaseApi) ExportScDiseaseRankExcel(c *gin.Context) {
	var pageInfo diseaseReq.DiseaseRank
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	pageInfo.ServiceCenterID = utils.GetUserID(c)
	if file, name, err := DiseaseService.ExportScDiseaseRankExcel(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", name+utils.RandomString(6)+".xlsx")) // 对下载的文件重命名
		c.Header("success", "true")
		c.Data(http.StatusOK, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file.Bytes())
	}
}

// GetDiseaseUserList 獲取病症用户列表【超管】
// @Tags GetDiseaseUserList
// @Summary 獲取病症用户列表【超管】
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /disease/getDiseaseUserList [get]
func (diseaseApi *DiseaseApi) GetDiseaseUserList(c *gin.Context) {
	var pageInfo diseaseReq.DiseaseUser
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := DiseaseService.GetDiseaseUserList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}

// GetPhysiqueSolar 獲取体質节气
// @Tags GetDiseaseList
// @Summary 獲取体質节气
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /disease/getPhysiqueSolar [get]
func (diseaseApi *DiseaseApi) GetPhysiqueSolar(c *gin.Context) {
	id := utils.GetUserID(c)
	// 獲取用户体質
	userPhysique, _ := AssessService.GetPhysiqueQuestionLog(map[string]interface{}{"uid": id}, "id,habitus,physique_id")
	if userPhysique.ID == 0 {
		response.FailWithMessage("请先做体質问卷", c)
	}
	// 獲取今日节气
	list, _ := DiseaseService.GetSolarTerms()
	response.Result(200,
		diseaseRes.PhysiqueSolarResult{
			Tz: diseaseRes.Tz{
				ID:     userPhysique.PhysiqueId,
				TzName: userPhysique.Physique + "体質",
			},
			Jq: diseaseRes.Jq{
				ID:     uint(list.ID),
				JqName: list.JqName,
			},
		}, "獲取成功", c)
}

// SaveReports 保存报告
// @Tags SaveReports
// @Summary 保存报告
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /disease/saveReports [post]
func (diseaseApi *DiseaseApi) SaveReports(c *gin.Context) {
	var dd disease.DiseaseReportRecord
	err := c.ShouldBindJSON(&dd)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"BzlxIDs": {utils.NotEmpty()},
		"BzIDs":   {utils.NotEmpty()},
	}
	if err := utils.Verify(dd, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	id := utils.GetUserID(c)
	dd.Uid = id
	if err := DiseaseService.SaveReports(dd); err != nil {
		global.GVA_LOG.Error("提交失敗!", zap.Error(err))
		response.FailWithMessage("提交失敗", c)
	} else {
		response.Result(200, "", "提交成功", c)
	}
}

// SaveDiseaseHistory 保存病史
// @Tags disease
// @Summary 保存病史
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /history/saveDiseaseHistory [post]
func (diseaseApi *DiseaseApi) SaveDiseaseHistory(c *gin.Context) {
	var du disease.UserDiseaseHistory
	err := c.ShouldBindJSON(&du)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Current":   {utils.NotEmpty()},
		"CMeasure":  {utils.NotEmpty()},
		"CDrugInfo": {utils.NotEmpty()},
		"Past":      {utils.NotEmpty()},
		"PMeasure":  {utils.NotEmpty()},
		"PDrugInfo": {utils.NotEmpty()},
	}
	if err := utils.Verify(du, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	id := utils.GetUserID(c)
	du.ID = id
	if err := DiseaseService.SaveDiseaseHistory(&du); err != nil {
		global.GVA_LOG.Error("提交失敗!", zap.Error(err))
		response.FailWithMessage("提交失敗", c)
	} else {
		response.Result(200, "", "提交成功", c)
	}
}

// GetUserDiseaseHistory 獲取用户病史
// @Tags disease
// @Summary 獲取用户病史
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /history/getUserDiseaseHistory [get]
func (diseaseApi *DiseaseApi) GetUserDiseaseHistory(c *gin.Context) {
	id := utils.GetUserID(c)
	if result, err := DiseaseService.GetUserDiseaseHistory(id); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage("獲取失敗", c)
	} else {
		response.Result(200, result, "獲取成功", c)
	}
}

// Check 验证是否可看我的方案
// @Tags disease
// @Summary 验证是否可看我的方案
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"獲取成功"}"
// @Router /disease/check [get]
func (diseaseApi *DiseaseApi) Check(c *gin.Context) {
	id := utils.GetUserID(c)
	if err := DiseaseService.Check(id); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.Ok(c)
	}
}

// GetBrain  獲取脑波
func (diseaseApi *DiseaseApi) GetBrain(c *gin.Context) {
	id := utils.GetUserID(c)
	rootID := c.Query("root_id")
	if article, isCheck, err := DiseaseService.GetBrain(id, rootID); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		if rootID == strconv.Itoa(int(article.ID)) {
			response.Result(7, gin.H{
				"article":  article,
				"isSelect": isCheck,
			}, "当前没有新的脑波方案", c)
		} else {
			response.OkWithData(gin.H{
				"article":  article,
				"isSelect": isCheck,
			}, c)
		}
	}
}

// GetSport 獲取运动
func (diseaseApi *DiseaseApi) GetSport(c *gin.Context) {
	id := utils.GetUserID(c)
	rootID := c.Query("root_id")
	if article, isCheck, err := DiseaseService.GetSport(id, rootID); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		if rootID == strconv.Itoa(int(article.ID)) {
			response.Result(7, gin.H{
				"article":  article,
				"isSelect": isCheck,
			}, "当前没有新的运动方案", c)
		} else {
			response.OkWithData(gin.H{
				"article":  article,
				"isSelect": isCheck,
			}, c)
		}
	}
}

// SetBrain 完成脑波
func (diseaseApi *DiseaseApi) SetBrain(c *gin.Context) {
	id := utils.GetUserID(c)
	var req catering.RootReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		return
	}
	if err = DiseaseService.SetBrain(id, strconv.Itoa(int(req.RootID))); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.Ok(c)
	}
}

// SetSport 完成运动
func (diseaseApi *DiseaseApi) SetSport(c *gin.Context) {
	id := utils.GetUserID(c)
	var req catering.RootReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		return
	}
	if err = DiseaseService.SetSport(id, strconv.Itoa(int(req.RootID))); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.Ok(c)
	}
}

// GetSchedule 獲取完成进度
func (diseaseApi *DiseaseApi) GetSchedule(c *gin.Context) {
	id := utils.GetUserID(c)
	if schedule, tips, err := DiseaseService.GetSchedule(id); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(gin.H{
			"schedule": schedule,
			"tips":     tips,
		}, c)
	}
}

// CreatePlanCompleteRecord 創建方案完成记录
func (diseaseApi *DiseaseApi) CreatePlanCompleteRecord(c *gin.Context) {
	uid := utils.GetUserID(c)
	if err := DiseaseService.CreatePlanCompleteRecord(uid); err != nil {
		global.GVA_LOG.Error("創建失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		// 添加完成方案积分相关
		err = UserService.AddPoints(uid, 10, 3)
		response.Result(200, "", "創建成功", c)
	}
}

// GetUserBasicInfo 獲取用户基本信息
func (diseaseApi *DiseaseApi) GetUserBasicInfo(c *gin.Context) {
	uid := utils.GetUserID(c)
	if list, err := DiseaseService.GetUserBasicInfo(uid); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.Result(200, list, "獲取成功", c)
	}
}

// GetUserList 獲取用户列表【服务站】
func (diseaseApi *DiseaseApi) GetUserList(c *gin.Context) {
	var pageInfo adminReq.UserList
	e := c.ShouldBindQuery(&pageInfo)
	if e != nil {
		response.FailWithMessage(e.Error(), c)
		return
	}
	id := utils.GetUserID(c)
	pageInfo.ServiceCenterID = id
	if list, total, err := DiseaseService.GetUserList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}

// GetAllUserList 獲取用户列表【超管】
func (diseaseApi *DiseaseApi) GetAllUserList(c *gin.Context) {
	var pageInfo diseaseReq.UserList
	e := c.ShouldBindQuery(&pageInfo)
	if e != nil {
		response.FailWithMessage(e.Error(), c)
		return
	}
	if list, total, err := DiseaseService.GetAllUserList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "獲取成功", c)
	}
}

// ExcelUserList 导出用户列表【超管】
func (diseaseApi *DiseaseApi) ExcelUserList(c *gin.Context) {
	var pageInfo adminReq.UserList
	e := c.ShouldBindQuery(&pageInfo)
	if e != nil {
		response.FailWithMessage(e.Error(), c)
		return
	}
	id := utils.GetUserID(c)
	pageInfo.ServiceCenterID = id
	if file, name, err := DiseaseService.ExcelUserList(pageInfo); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", name+utils.RandomString(6)+".xlsx")) // 对下载的文件重命名
		c.Header("success", "true")
		c.Data(http.StatusOK, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file.Bytes())
	}
}

// ExcelScUserList 导出用户列表【服务中心】
func (diseaseApi *DiseaseApi) ExcelScUserList(c *gin.Context) {
	var pageInfo adminReq.UserList
	e := c.ShouldBindQuery(&pageInfo)
	if e != nil {
		response.FailWithMessage(e.Error(), c)
		return
	}
	ScID := utils.GetUserID(c)
	if file, name, err := DiseaseService.ExcelScUserList(pageInfo, ScID); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", name+utils.RandomString(6)+".xlsx")) // 对下载的文件重命名
		c.Header("success", "true")
		c.Data(http.StatusOK, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file.Bytes())
	}
}

// GetUserRecord 獲取对应方案
func (diseaseApi *DiseaseApi) GetUserRecord(c *gin.Context) {
	userID := c.Query("userID")
	flag := c.Query("flag")
	if plan, err := DiseaseService.GetUserRecord(userID, flag); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithDetailed(plan, "獲取成功", c)
	}
}

// SetUserRecord 獲取修改对应方案
func (diseaseApi *DiseaseApi) SetUserRecord(c *gin.Context) {
	var plan disease.DiseaseReportPlan
	flag := c.Query("flag")
	err := c.ShouldBindJSON(&plan)
	if err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := DiseaseService.SetUserRecord(plan, flag); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.Ok(c)
	}
}

// SetUserRecord 獲取修改对应方案
func (diseaseApi *DiseaseApi) GetArticle(c *gin.Context) {
	if res, err := DiseaseService.GetArticle(); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(res, c)
	}
}

// SetPlanTips 保存方案提醒
// @Router /healthManager/savePlanTips [post]
func (diseaseApi *DiseaseApi) SetPlanTips(c *gin.Context) {
	var cf catering.Food
	err := c.ShouldBindJSON(&cf)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Tips": {utils.NotEmpty()},
	}
	if err := utils.Verify(cf, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := DiseaseService.SetPlanTips(&cf); err != nil {
		global.GVA_LOG.Error("保存失敗", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("保存成功", c)
	}
}

func (diseaseApi *DiseaseApi) OpenVip(c *gin.Context) {
	ssID := utils.GetUserID(c)
	id := c.Query("id")
	vipType := c.Query("vipType")
	authority := utils.GetUserAuthorityId(c)
	if err := DiseaseService.OpenVip(ssID, id, authority, vipType); err != nil {
		global.GVA_LOG.Error("开通失敗", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("开通成功", c)
	}
}

func (diseaseApi *DiseaseApi) UserHealth(c *gin.Context) {
	id := c.Query("id")
	if list, err := DiseaseService.UserHealth(id); err != nil {
		global.GVA_LOG.Error("失敗", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(list, c)
	}
}

func (diseaseApi *DiseaseApi) SetUserPoints(c *gin.Context) {
	id := c.Query("id")
	points := c.Query("points")
	if err := DiseaseService.SetUserPoints(id, points); err != nil {
		global.GVA_LOG.Error("失敗", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.Ok(c)
	}
}

func (diseaseApi *DiseaseApi) GetHealthData(c *gin.Context) {
	id := c.Query("id")
	if datas, err := DiseaseService.GetHealthData(id); err != nil {
		global.GVA_LOG.Error("失敗", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(datas, c)
	}
}

// GetHisPlan 獲取他的方案
// @Router /hisPlan/getHisPlan [get]
func (diseaseApi *DiseaseApi) GetHisPlan(c *gin.Context) {
	id := c.Query("id")
	types := c.Query("types")
	selectTypes := c.Query("select_types")
	if datas, err := DiseaseService.GetHisPlan(id, types, selectTypes); err != nil {
		global.GVA_LOG.Error("失敗", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(datas, c)
	}
}

// SaveHisPlan 保存他的方案
// @Router /hisPlan/saveHisPlan [put]
func (diseaseApi *DiseaseApi) SaveHisPlan(c *gin.Context) {
	var plan disease.DiseaseReportPlan
	err := c.ShouldBindJSON(&plan)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := DiseaseService.SetHisPlan(plan); err != nil {
		global.GVA_LOG.Error("獲取失敗!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.Ok(c)
	}

}

// GetHisBalancePlan 獲取他的平衡方案
// @Router /hisPlan/getHisBalancePlan [get]
func (diseaseApi *DiseaseApi) GetHisBalancePlan(c *gin.Context) {
	id := c.Query("id")
	if data, err := BodyService.GetHisBalancePlan(id); err != nil {
		global.GVA_LOG.Error("獲取失敗", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(data, c)
	}
}

// SaveHisBalancePlan 保存他的平衡方案
// @Router /hisPlan/saveHisBalancePlan [put]
func (diseaseApi *DiseaseApi) SaveHisBalancePlan(c *gin.Context) {
	var bb body.HealthManageTag
	err := c.ShouldBindJSON(&bb)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Uid":          {utils.NotEmpty()},
		"PlaceTypes":   {utils.NotEmpty()},
		"Place":        {utils.NotEmpty()},
		"FasciaChain":  {utils.NotEmpty()},
		"CorrectCycle": {utils.NotEmpty()},
	}
	if err := utils.Verify(bb, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	bodyInfo, _ := BodyService.GetHealthManageTagByUid(bb.Uid, 1)
	// 判断是否存在用户功效标签
	if bodyInfo.ID != 0 {
		if err := BodyService.UpdateHealthManageTag(bodyInfo.ID, &bb); err != nil {
			global.GVA_LOG.Error("保存失敗!", zap.Error(err))
			response.FailWithMessage("保存失敗", c)
		}
	} else {
		bb.Types = 1
		if err := BodyService.CreateHealthManageTag(&bb); err != nil {
			global.GVA_LOG.Error("保存失敗!", zap.Error(err))
			response.FailWithMessage("保存失敗", c)
		}
	}
	response.Result(200, "", "保存成功", c)
}
